import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:todo_app/models/todo_model.dart';
import 'package:todo_app/pages/providers/todo_filter/todo_filter_provider.dart';
import 'package:todo_app/pages/providers/todo_list/todo_list_provider.dart';
import 'package:todo_app/pages/providers/todo_search/todo_search_provider.dart';

part 'filtered_todos_provider.g.dart';

@riverpod
List<Todo> filteredTodos(Ref ref) {
  final todoFilter = ref.watch(todoFilterProvider);
  final todoList = ref.watch(todoListProvider);
  final todoSearch = ref.watch(todoSearchProvider);

  List<Todo> tempTodos;

  tempTodos = switch (todoFilter) {
    Filter.all => todoList,
    Filter.active => todoList.where((todo) => !todo.completed).toList(),
    Filter.completed => todoList.where((todo) => todo.completed).toList(),
  };
  tempTodos =
      tempTodos
          .where((todo) => todo.desc.toLowerCase().contains(todoSearch))
          .toList();

  return tempTodos;
}
